﻿<Application x:Class="Mach.Kinectomix.LevelEditor.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:ViewModel="clr-namespace:Mach.Kinectomix.LevelEditor.ViewModel"
             xmlns:Behavior="clr-namespace:Mach.Kinectomix.LevelEditor.Behavior"
             xmlns:Converter="clr-namespace:Mach.Kinectomix.LevelEditor.Converter"
             xmlns:l="clr-namespace:Mach.Kinectomix.LevelEditor"
             xmlns:localization="clr-namespace:Mach.Kinectomix.LevelEditor.Localization"
             xmlns:properties="clr-namespace:Mach.Kinectomix.LevelEditor.Properties"
             StartupUri="View/EditorWindow.xaml">
    <Application.Resources>
        <localization:LocalizedStrings xmlns:local="clr-namespace:Mach.Kinectomix.LevelEditor.Localization" x:Key="LocalizedStrings" />

        <Converter:NegateConverter x:Key="NegateConverter" />
        <Converter:AllTrueConveter x:Key="AllTrueConveter" />
        <Converter:NullToTrueConverter x:Key="NullToTrueConverter" />
        <Converter:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

        <DataTemplate x:Key="ViewOnlyBoard" DataType="{x:Type ViewModel:BoardViewModel}">
            <Viewbox Stretch="Uniform">
                <ItemsControl ItemsSource="{Binding Tiles}" 
                          Margin="3">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Image Source="Board/Empty.png"
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center"
                                       Width="49"
                                       Height="49"
                                       Stretch="Uniform"/>
                                <l:BondsVisualiser TopLeftBond="{Binding TopLeftBond}"
                                                       TopBond="{Binding TopBond}"
                                                       TopRightBond="{Binding TopRightBond}" 
                                                       RightBond="{Binding RightBond}" 
                                                       BottomRightBond="{Binding BottomRightBond}" 
                                                       BottomBond="{Binding BottomBond}"
                                                       BottomLeftBond="{Binding BottomLeftBond}"
                                                       LeftBond="{Binding LeftBond}" />
                                <Image Source="{Binding AssetSource}" 
                                       Behavior:TilePainter.IsPaintEnabled="False"
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center"
                                       Width="49"
                                       Height="49"
                                       Stretch="Uniform">
                                    <Image.Style>
                                        <Style TargetType="Image">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsPreview}" Value="True">
                                                    <Setter Property="Opacity" Value="0.5"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="{Binding Tiles.ColumnsCount}"
                                         Rows="{Binding Tiles.RowsCount}"
                                         VerticalAlignment="Center"
                                         HorizontalAlignment="Center" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </Viewbox>
        </DataTemplate>

        <DataTemplate DataType="{x:Type ViewModel:BoardViewModel}">
            <ScrollViewer HorizontalScrollBarVisibility="Auto"
                          VerticalScrollBarVisibility="Auto">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <Grid.Resources>
                        <Style TargetType="{x:Type Button}">
                            <Setter Property="Width" Value="25" />
                            <Setter Property="Height" Value="25" />
                            <Setter Property="Margin" Value="3" />
                        </Style>
                    </Grid.Resources>

                    <StackPanel Grid.Row="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Bottom" Orientation="Horizontal">
                        <Button Content="+" Command="{Binding InsertRowToTopCommand}" />
                        <Button Content="-" Command="{Binding RemoveRowFromTopCommand}" />
                    </StackPanel>

                    <StackPanel Grid.Row="2" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Top" Orientation="Horizontal">
                        <Button Content="+" Command="{Binding InsertRowToBottomCommand}" />
                        <Button Content="-" Command="{Binding RemoveRowFromBottomCommand}" />
                    </StackPanel>

                    <StackPanel Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">
                        <Button Content="+" Command="{Binding InsertColumnToLeftCommand}"/>
                        <Button Content="-" Command="{Binding RemoveColumnFromLeftCommand}"/>
                    </StackPanel>

                    <StackPanel Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Left">
                        <Button Content="+" Command="{Binding InsertColumnToRightCommand}" />
                        <Button Content="-" Command="{Binding RemoveColumnFromRightCommand}" />
                    </StackPanel>

                    <ItemsControl ItemsSource="{Binding Tiles}" 
                                  Grid.Row="1"
                                  Grid.Column="1"
                                  Margin="3">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ToolTip>
                                        <ToolTip Visibility="{Binding Path=ShowAssetToolTips, Source={x:Static properties:Settings.Default}, Converter={StaticResource BooleanToVisibilityConverter}}" 
                                                 Content="{Binding Asset}" />
                                    </Grid.ToolTip>
                                    <Image Source="Board/Empty.png"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           Width="49"
                                           Height="49"
                                           Stretch="Fill"/>
                                    <l:BondsVisualiser TopLeftBond="{Binding TopLeftBond}"
                                                       TopBond="{Binding TopBond}"
                                                       TopRightBond="{Binding TopRightBond}" 
                                                       RightBond="{Binding RightBond}" 
                                                       BottomRightBond="{Binding BottomRightBond}" 
                                                       BottomBond="{Binding BottomBond}"
                                                       BottomLeftBond="{Binding BottomLeftBond}"
                                                       LeftBond="{Binding LeftBond}" />
                                    <Image Source="{Binding AssetSource}" 
                                           Behavior:TilePainter.IsPaintEnabled="True"
                                           Behavior:TilePainter.PaintTile="{Binding Path=DataContext.PaintTile, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           Width="49"
                                           Height="49"
                                           Stretch="Fill">
                                        <Behavior:TileBonds.IsBondingEnabled>
                                            <MultiBinding Converter="{StaticResource AllTrueConveter}">
                                                <Binding Path="IsFixed" Converter="{StaticResource NegateConverter}" />
                                                <Binding Path="IsPreview" Converter="{StaticResource NegateConverter}" />
                                                <Binding Path="DataContext.PaintTile" RelativeSource="{RelativeSource AncestorType=ItemsControl}" Converter="{StaticResource NullToTrueConverter}" />
                                            </MultiBinding>
                                        </Behavior:TileBonds.IsBondingEnabled>
                                        <Image.Style>
                                            <Style TargetType="Image">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IsPreview}" Value="True">
                                                        <Setter Property="Opacity" Value="0.5"/>
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Image.Style>
                                    </Image>
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="{Binding Tiles.ColumnsCount}"
                             Rows="{Binding Tiles.RowsCount}"
                             VerticalAlignment="Center"
                             HorizontalAlignment="Center"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </Grid>
            </ScrollViewer>
        </DataTemplate>

        <DataTemplate DataType="{x:Type ViewModel:AvailableTilesViewModel}">
            <StackPanel>
                <TextBlock Text="{Binding Editor.AvailableTiles, Source={StaticResource LocalizedStrings}}" 
                           Tag="{Binding DataContext,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type StackPanel}}}"
                           FontWeight="Bold">
                    <TextBlock.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="{Binding Editor.OpenAtomsAssetsDirectory, Source={StaticResource LocalizedStrings}}" 
                                                  Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.Tag.OpenAtomsAssetsDirectoryCommand}" 
                                                  CommandParameter="{Binding}" />
                            <MenuItem Header="{Binding Editor.OpenFixedAssetsDirectory, Source={StaticResource LocalizedStrings}}" 
                                                  Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.Tag.OpenFixedAssetsDirectoryCommand}" 
                                                  CommandParameter="{Binding}" />
                            <MenuItem Header="{Binding Editor.ReloadUserAssets, Source={StaticResource LocalizedStrings}}" 
                                                  Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.Tag.ReloadUserAssetsCommand}" 
                                                  CommandParameter="{Binding}" />
                        </ContextMenu>
                    </TextBlock.ContextMenu>
                </TextBlock>

                <ListBox Background="Transparent" BorderThickness="0" ItemsSource="{Binding AvailableTiles}" SelectedItem="{Binding SelectedTile}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.Template>
                        <ControlTemplate TargetType="ListBox">
                            <Border>
                                <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
                                    <ItemsPresenter/>
                                </ScrollViewer>
                            </Border>
                        </ControlTemplate>
                    </ListBox.Template>
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}">
                            <Setter Property="Padding" Value="0,0,0,0"/>
                            <Setter Property="Margin" Value="1"/>
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <ToggleButton IsChecked="{Binding 
                                              RelativeSource={RelativeSource FindAncestor, 
                                              AncestorType={x:Type ListBoxItem}},
                                              Path=IsSelected}"
                                              Background="Transparent"
                                              Margin="0"
                                              BorderThickness="0">
                                <Border BorderBrush="Black">
                                    <Border.Style>
                                        <Style TargetType="Border">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding IsFromLevel}" Value="True">
                                                    <Setter Property="BorderThickness" Value="1"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Border.Style>
                                    <Grid Margin="3">
                                        <Grid.ToolTip>
                                            <ToolTip Visibility="{Binding Path=ShowAssetToolTips, Source={x:Static properties:Settings.Default}, Converter={StaticResource BooleanToVisibilityConverter}}" 
                                                 Content="{Binding Asset}" />
                                        </Grid.ToolTip>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition />
                                        </Grid.RowDefinitions>


                                        <Image Grid.Row="0" Width="49" Height="49" Stretch="Fill" Source="{Binding AssetSource}" />
                                        <TextBlock Grid.Row="1" Text="{Binding Name}" TextAlignment="Center"/>
                                    </Grid>
                                </Border>
                            </ToggleButton>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>

        </DataTemplate>
    </Application.Resources>
</Application>
